<?php
/**
* html, head, body.
*
*/
class page extends html
{
    public $div;

    public function page($title=NULL, $inner=NULL, $bodyClass=NULL)
    {
        parent::html();
        $this->inner['head'] = new head();
        $this->inner['head']->inner['meta']     = new Meta();
        $this->inner['head']->inner['title']    = new Title( $title );
        $this->inner['body']                    = new body($inner, $bodyClass);
        $this->inner['body']->inner['form']     = new Form('formSite',null, 'index.php','post' );
        $this->inner['body']->inner['form']->addInner( new Div('response') );
        $this->inner['body']->inner['script']   = new Script();
    }

    public function onLoad( $javascript )
    {
        $this->inner['body']->addAttribute('onload', $javascript );
    }

    public function setTitle( $title )
    {
        $this->inner['head']->inner['title']->setInnerHTML( $title );
    }

    public function addContent($inner)
    {
       $this->inner['body']->inner['form']->addInner($inner);
    }

    public function setContent($inner)
    {
        $this->inner['body']->inner['form']->setInnerHTML($inner);
    }

    public function addCss($href, $rel='stylesheet' , $type='text/css', $media=NULL)
    {
        //$this->addInner( new StyleFile($href, $rel, $type, $media) );
        //temporary to XHTML 1.1 Transitional
        $this->inner['head']->addInner(new StyleFile($href, $rel, $type, $media));
    }

    public function addScript($src=NULL, $content=NULL, $type="text/javascript")
    {
        //$this->addInner( new ScriptFile($src, $content, $type ) );
        //temporary to XHTML 1.1 Transitional
        $this->inner['head']->addInner( new ScriptFile($src, $content, $type ) );
    }

    public function addJsCode($content=NULL)
    {
        $this->inner['body']->inner['script']->addJsCode($content);
    }

    public function __toString()
    {
        if ( $_REQUEST['ajax'] != 'true' || $_REQUEST['ajax'] != true)
        {
	        $this->xmlns = 'http://www.w3.org/1999/xhtml';
	        $this->dir   = 'ltr';
	        $this->lang  = 'pt-BR';
	        $temp = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
	        $temp .= parent::__toString();
	        return $temp;
        }
        else
        {
            callAJaxFunction(); //ajaxEventHandler, has to be done after all includes
            return '';
        }
    }
}

function callAJaxFunction()
{
    if ( $_REQUEST['ajax'] == 'true' && $_REQUEST['phpFunction'] != '')
    {
        $function  = $_REQUEST['phpFunction'];
        $temp      = explode(':', $function);
        $function  = $temp[count($temp)-1];
        $class     = $temp[count($temp)-2];

        $requestObject = (Object) $_REQUEST;

        //call object
        if ( $class )
        {
            $obj   = new $class( $requestObject );

            if ( $function == '__construct')
            {
                echo $obj;
                return;
            }

            if ( method_exists( $obj, $function ) )
            {
                $result= $obj->$function( $requestObject );
            }
            else
            {
                $result = array( "Method <b>$class->$function</b> don't exists." );
            }
        }
        //call only a static function
        else
        {
            if ( function_exists( $function ) )
            {
                $result    = $function( $requestObject );
            }
            else
            {
                $result = array( "Function <b>$function</b> don't exists." );
            }
        }

        echo implode('' , is_array($result) ? $result : array( $result ) );
    }
}
?>
